<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\genQuestionGroup\controller;

use plugin\saiadmin\basic\BaseController;
use app\genQuestionGroup\logic\SkiQuestionGroupListLogic;
use app\genQuestionGroup\validate\SkiQuestionGroupListValidate;
use support\Log;
use support\Request;
use support\Response;
use think\facade\Db;

/**
 * 问题分组控制器
 */
class SkiQuestionGroupListController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new SkiQuestionGroupListLogic();
        $this->validate = new SkiQuestionGroupListValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['groupName', ''],
            ['lang', ''],
            ['appType', ''],
        ]);
        $query = $this->logic->search($where);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 不需要登录的方法
     */
    protected array $noNeedLogin = ['getAllGroup', 'getAllGroupFix'];

    // 获取所有的问题分类
    public function getAllGroup(Request $request): Response
    {
        $where = $request->more([
            ['lang', ''],
            ['appType', ''],
        ]);
        $query = $this->logic->search($where);
        $list = $this->logic->getAll($query);

        $result = array();
        foreach ($list as $index => $item) {
            $matchIndex = Array_findIndex($result, function ($val) use ($item) {
                return $val['appType'] == $item['appType'] && $item['lang'] == $val['lang'];
            });

            if ($matchIndex == -1) {
                $result[] = [
                    'appType' => $item['appType'],
                    'lang' => $item['lang'],
                    'list' => [$item]
                ];
            } else {
                $result[$matchIndex]['list'][] = $item;
            }
        }

        return $this->success($result);
    }

    // 获取所有的问题分类： 使用关联数组的特性来优化（只能用于特例），避免使用自定义的 findIndex 方法
    public function getAllGroupFix(Request $request): Response
    {
        $where = $request->more([
            ['groupName', ''],
            ['lang', ''],
            ['appType', ''],
        ]);
        $query = $this->logic->search($where);
        $list = $this->logic->getAll($query);

        $result = [];
        foreach ($list as $item) {
            // 使用 appType 和 lang 组合作为键名，确保唯一性
            $key = "{$item['appType']}_{$item['lang']}";

            if (!isset($result[$key])) {
                $result[$key] = [
                    'appType' => $item['appType'],
                    'lang' => $item['lang'],
                    'list' => []
                ];
            }

            $result[$key]['list'][] = $item;
        }

        // 将关联数组转为索引数组（如果需要）
        $result = array_values($result);
        return $this->success($result);
    }
}
